package Limit.Skill.Jewelry;

import l1j.server.server.model.Instance.L1ItemInstance;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.serverpackets.S_SystemMessage;

public class E_Jewelry {
	private static int _damagereduction;
	private static int _magicreduction;
	private static int _doubledmgchance;
	private static int _magicdmgmodifier;
	private static int _dmgmodifier;
	private static int _bowdmgmodifier;
	private static int _hitmodifier ;
	private static int _bowhitmodifier ;
	private static int _regisstun;
	private static int _regisstone;
	private static int _regisfreeze;
	private static int _regissustain;
	private static int _hpr;
	private static int _mpr;
	private static int _hp;
	private static int _mp;
	private static int _firemr;
	private static int _watermr;
	private static int _earthmr;
	private static int _windmr;
	
	public void JewelryReset(L1ItemInstance item, L1ItemInstance item1, L1PcInstance pc){
		int type1 = item1.getJewelryType1();
		int itemtype = item1.getItem().getType();
		int lv = item1.getJewelryLv1();
		
		switch (type1){
		case 1:
			switch (itemtype){
			case 6: case 7:
				_hp = 0;
				if (item1.isEquipped()) {
					pc.addMaxHp(- (lv * 10));
				}
				break;
			}		
			break;
		case 2:
			switch (itemtype){
			case 2: case 7:
				_hpr = 0;
				if (item1.isEquipped()) {
					pc.addHpr(- (lv * 5));
				}
				break;
			}		
			break;
		case 3:
			switch (itemtype){
			case 2: case 7:
				_mpr = 0;
				if (item1.isEquipped()) {
					pc.addMpr(- (lv * 3));
				}
				break;
			}
		case 4:
			switch (itemtype){
			case 6: case 7:
				_mp = 0;
				if (item1.isEquipped()) {
					pc.addMaxMp(- (lv * 10));
				}
				break;
			}
			break;
		}
		if (itemtype == 2 || itemtype == 7){
			int type2 = item1.getJewelryType2();
			int lv2 = item1.getJewelryLv2();
			switch (type2){
			case 1:
				switch (itemtype){
				case 6: case 7:
					_hp = 0;
					if (item1.isEquipped()) {
						pc.addMaxHp(- (lv2 * 10));
					}
					break;
				}		
				break;
			case 2:
				switch (itemtype){
				case 2: case 7:
					_hpr = 0;
					if (item1.isEquipped()) {
						pc.addHpr(- (lv2 * 5));
					}
					break;
				}		
				break;
			case 3:
				switch (itemtype){
				case 2: case 7:
					_mpr = 0;
					if (item1.isEquipped()) {
						pc.addMpr(- (lv2 * 3));
					}
					break;
				case 5: case 10:
					_magicdmgmodifier = 0;
					pc.addMagicDmgModifier(_magicdmgmodifier);
					break;
				}
			case 4:
				switch (itemtype){
				case 6: case 7:
					_mp = 0;
					if (item1.isEquipped()) {
						pc.addMaxMp(- (lv2 * 10));
					}
					break;
				}
				break;
			}
		}
		item1.setJewelryLv1(0);
		item1.setJewelryLv2(0);
		item1.setJewelryType1(0);
		item1.setJewelryType2(0);
		item1.setJewelryUse(0);
		item1.setDamageReduction(0);
		item1.setMagicReduction(0);
		item1.setDoubleDmgChance(0);
		item1.setMagicDmgModifier(0);
		item1.setDmgModifier(0);
		item1.setBowDmgModifier(0);
		item1.setHitModifier(0);
		item1.setBowHitModifier(0);
		item1.setRegisStun(0);
		item1.setRegisStone(0);
		item1.setRegisFreeze(0);
		item1.setRegisSustain(0);
		item1.setHpr(0);
		item1.setMpr(0);
		item1.setaddHp(0);
		item1.setaddMp(0);
		item1.setFireMr(0);
		item1.setWaterMr(0);
		item1.setEarthMr(0);
		item1.setWindMr(0);
	}
	
	public void Jewelry(L1ItemInstance item, L1ItemInstance item1, L1PcInstance pc) {
		L1ItemInstance check_item = pc.getInventory().getItem(item1.getId());
		int type = 0;
		int lv = 0;
		if (check_item != null && check_item.getItem().getType2() == 2
				&& ((check_item.getItem().getType() >= 1 && check_item.getItem().getType() <= 7)
				|| check_item.getItem().getType() == 10)){ 
			int itemid = item.getItemId();
			switch (itemid){
			case 70119: case 70120: case 70121: case 70122: case 70123:
			case 70124: case 70125: case 70126: case 70127: case 70128:
				type = 1;
				lv = itemid-70118;
				break;
			case 70129: case 70130: case 70131: case 70132: case 70133:
			case 70134: case 70135: case 70136: case 70137: case 70138:
				type = 2;
				lv = itemid-70128;
				break;
			case 70139: case 70140: case 70141: case 70142: case 70143:
			case 70144: case 70145: case 70146: case 70147: case 70148:
				type = 3;
				lv = itemid-70138;
				break;
			case 70149: case 70150: case 70151: case 70152: case 70153:
			case 70154: case 70155: case 70156: case 70157: case 70158:
				type = 4;
				lv = itemid-70148;
				break;
			}	
			if(item1.getJewelryUse() == 0){
				item1.setJewelryUse(1);
				item1.setJewelryLv1(lv);
				item1.setJewelryType1(type);
				runJewelry(item1, itemid, lv, type, check_item.getItem().getType(), pc);
				pc.getInventory().removeItem(item, 1);
			} else if (item1.getJewelryUse() == 1){
				if (check_item.getItem().getType() == 2 || check_item.getItem().getType() == 7){
					item1.setJewelryUse(2);
					item1.setJewelryLv2(lv);
					item1.setJewelryType2(type);
					runJewelry(item1, itemid, lv, type, check_item.getItem().getType(), pc);
					pc.getInventory().removeItem(item, 1);
				} else {
					pc.sendPackets(new S_SystemMessage("此裝備寶石數目已達上限。"));
				}
			} else {
				pc.sendPackets(new S_SystemMessage("此裝備寶石數目已達上限。"));
			}
		} else 
			pc.sendPackets(new S_SystemMessage("此裝備不能使用珠寶。"));
	}
	
	private void runJewelry(L1ItemInstance item1, int itemid, int lv, int type, int itemtype, L1PcInstance pc){
		_damagereduction = 0;
		_magicreduction = 0;
		_doubledmgchance = 0;
		_magicdmgmodifier = 0;
		_dmgmodifier = 0;
		_bowdmgmodifier = 0;
		_hitmodifier  = 0;
		_bowhitmodifier  = 0;
		_regisstun = 0;
		_regisstone = 0;
		_regisfreeze = 0;
		_regissustain = 0;
		_hpr = 0;
		_mpr = 0;
		_hp = 0;
		_mp = 0;
		_firemr = 0;
		_watermr = 0;
		_earthmr = 0;
		_windmr = 0;
		if (item1.getJewelryUse() == 1){
			switch (type){
			case 1:
				switch (itemtype){
				case 1: case 2: case 10:
					_damagereduction = lv;
					break;
				case 6: case 7:
					_hp = 10 * lv;
					if (item1.isEquipped()) {
						pc.addMaxHp(_hp);
					}
					break;
				case 4:
					_firemr = lv;
					break;
				case 3:
					_regisstone = lv;
					break;
				case 5:
					_dmgmodifier = lv;			
					break;
				}		
				break;
			case 2:
				switch (itemtype){
				case 2: case 7:
					_hpr = 5 * lv;
					if (item1.isEquipped()) {
						pc.addHpr(_hpr);
					}
					break;
				case 5: case 10:
					_doubledmgchance = lv;
					break;
				case 1: case 6:
					_hitmodifier = lv;
					break;
				case 4:
					_earthmr = lv;
					break;
				case 3:
					_regissustain = lv;			
					break;
				}		
				break;
			case 3:
				switch (itemtype){
				case 2: case 7:
					_mpr = lv * 3;
					if (item1.isEquipped()) {
						pc.addMpr(_mpr);
					}
					break;
				case 5: case 10:
					_magicdmgmodifier = lv;
					pc.addMagicDmgModifier(_magicdmgmodifier);
					break;
				case 1: case 6:
					_bowhitmodifier = lv;
					break;
				case 4:
					_windmr = lv;
					break;
				case 3:
					_regisstun = lv;
					break;
				}
				break;
			case 4:
				switch (itemtype){
				case 1: case 2: case 10:
					_magicreduction = lv;
					break;
				case 6: case 7:
					_mp = 10 * lv;
					if (item1.isEquipped()) {
						pc.addMaxMp(_mp);
					}
					break;
				case 4:
					_watermr = lv;
					break;
				case 3:
					_regisfreeze = lv;
					break;
				case 5:
					_bowdmgmodifier = lv;
					break;
				}
				break;
			}
		}
		else if (item1.getJewelryUse() == 2){
			switch (type){
			case 1:
				switch (itemtype){
				case 2:
					_damagereduction = item1.getDamageReduction() + lv;
					break;
				case 7:
					_hp = item1.getaddHp() + (10 * lv);
					if (item1.isEquipped()) {
						pc.addMaxHp(10 * lv);
					}
					break;
				}		
				break;
			case 2:
				switch (itemtype){
				case 2: case 7:
					_hpr = item1.getHpr() + (5 * lv);
					if (item1.isEquipped()) {
						pc.addHpr(5 * lv);
					}
					break;
				}		
				break;
			case 3:
				switch (itemtype){
				case 2: case 7:
					_mpr = item1.getMpr() + (lv * 3);
					if (item1.isEquipped()) {
						pc.addMpr(lv * 3);
					}
					break;
				}
			case 4:
				switch (itemtype){
				case 2:
					_magicreduction = item1.getMagicReduction() + lv;
					break;
				case 7:
					_mp = item1.getaddMp() + (10 * lv);
					if (item1.isEquipped()) {
						pc.addMaxMp(10 * lv);
					}
					break;
				}
				break;
			}
		}
		
		item1.setDamageReduction(_damagereduction);
		item1.setMagicReduction(_magicreduction);
		item1.setDoubleDmgChance(_doubledmgchance);
		item1.setMagicDmgModifier(_magicdmgmodifier);
		item1.setDmgModifier(_dmgmodifier);
		item1.setBowDmgModifier(_bowdmgmodifier);
		item1.setHitModifier(_hitmodifier );
		item1.setBowHitModifier(_bowhitmodifier );
		item1.setRegisStun(_regisstun);
		item1.setRegisStone(_regisstone);
		item1.setRegisFreeze(_regisfreeze);
		item1.setRegisSustain(_regissustain);
		item1.setHpr(_hpr);
		item1.setMpr(_mpr);
		item1.setaddHp(_hp);
		item1.setaddMp(_mp);
		item1.setFireMr(_firemr);
		item1.setWaterMr(_watermr);
		item1.setEarthMr(_earthmr);
		item1.setWindMr(_windmr);
	}
}
